var sex=true;
var age=true;
var spec = false;
$(function () {
    var token = $("#token").val();
    if (isempty(token))
        token = "null";
    var jsonstr = "{token:" + token + "}";
    var shopid = $("#shopid").val();
    var url = "/except/shopcontroller/shopinfo.do";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            json: jsonstr,
            shopid: shopid
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            if (code == 20000) {
                $("#shop_name").html(data.data.shop_name);
                $("#shop_img").attr("src", data.data.specprice[0].img);
                $("#shop_weight").html(data.data.shop_weight);
                var usertype = data.data.usertype;

                if(usertype * 1 == 1)
                    $("#shop_price").html(data.data.specprice[0].first);
                else if(usertype * 1 == 2)
                    $("#shop_price").html(data.data.specprice[0].second);
                else if(usertype * 1 == 3)
                    $("#shop_price").html(data.data.specprice[0].third);
                else if(usertype * 1 == 4)
                    $("#shop_price").html(data.data.specprice[0].forth);
                else if(usertype * 1 == 5)
                    $("#shop_price").html(data.data.specprice[0].fifth);
                else
                    $("#shop_price").html(data.data.specprice[0].price);

                $("#shop_expired").html(data.data.expired);
                var badyage = data.data.badyage;
                $(badyage).each(function () {
                    if (this.minage * 1 > this.maxage * 1)
                        $("#bady_age").append('<li onclick="chose_bady_age(\'' + this.bady_age_id + '\')">' + this.minage + '月以上</li>');
                    else
                        $("#bady_age").append('<li onclick="chose_bady_age(\'' + this.bady_age_id + '\')">' + this.minage + '-' + this.maxage + '月</li>');
                });
                var specprice = data.data.specprice;
                $(specprice).each(function () {
                    var price = 0;
                    if(usertype * 1 == 1)
                        price = this.first;
                    else if(usertype * 1 == 2)
                        price = this.second;
                    else if(usertype * 1 == 3)
                        price = this.third;
                    else if(usertype * 1 == 4)
                        price = this.forth;
                    else if(usertype * 1 == 5)
                        price = this.fifth;
                    else
                        price = this.price;
                    $("#bady_spec").append('<li onclick="chose_spec(\'' + this.img + '\',\'' + price + '\', \'' + this.shop_spec_price_id + '\',\'' + this.name + '\')">' + this.name + '</li>');
                });
                var shopfavorabled = data.data.shopfavorabled;
                $(shopfavorabled).each(function () {
                    $("#shopfavorabled").append('<span style="margin-left: 3px;background-color: #ff3b43;color: white;font-size: 12px;padding: 2px 3px; border-radius: 6px;">满' + this.number + '件' + this.lose + '折</span>');
                    // $("#shopfavorabled").append('<span style="margin-left: 3px; background-color: #ff3b43;color: white;font-size: 13px;padding: 2px 3px;border-radius: 6px;">满' + this.number + '件' + this.lose + '折</span>');
                });
                remove_advertising();
            }
        }
    });

    // $("#bady_sex li").each(function () {
    //     $(this).click(function () {
    //         sex = true;
    //         $("#bady_sex li").removeClass("badysexageActive")
    //         $(this).addClass("badysexageActive");
    //         chose_result();
    //     });
    // });
    // $("#bady_age li").each(function () {
    //     $(this).click(function () {
    //         age = true;
    //         $("#bady_age li").removeClass("badysexageActive")
    //         $(this).addClass("badysexageActive");
    //         chose_result();
    //     });
    // });
    $("#bady_spec li").each(function () {
        $(this).click(function () {
            spec = true;
            $("#bady_spec li").removeClass("badysexageActive")
            $(this).addClass("badysexageActive");
            chose_result();
        });
    });
});

/**
 * 选中商品规格
 * @param img
 * @param price
 */
function chose_spec(img, price, specid, specname) {
    $("#shop_img").attr("src", img);
    $("#shop_price").html(price);
    $("#shop_spec_id").val(specid);
    $("#specname").val(specname);
}

/**
 * 选中bady性别
 * @param sex
 */
function chose_sex(sex) {
    $("#bady_sex_id").val(sex);
}

/**
 * 选择bady年龄
 * @param bady_age_id
 */
function chose_bady_age(bady_age_id) {
    $("#badyageid").val(bady_age_id);
}

/**
 * 商品数量减少
 */
function minus() {
    var shop_number = $("#shop_number").html();
    if (shop_number * 1 > 1)
        $("#shop_number").html(shop_number * 1 - 1);
}

/**
 * 商品数量增加
 */
function addnum() {
    var shop_number = $("#shop_number").html();
    $("#shop_number").html(shop_number * 1 + 1);
}

/**
 * 选择结果
 */
function chose_result() {
    if (spec) {
        $("#add_chat").removeClass("threeActive_not").addClass("threeActive");
        $("#add_order").removeClass("fourActive_not").addClass("fourActive");
    }
}

function add_chat() {
    if (spec) {
        var token = $("#token").val();
        if (isempty(token)) {
            location.href = "/except/login.jsp";
            return;
        }
        var jsonstr = "{token:" + token + "}";
        var bady_sex_id = 1;
        var badyageid = 1;
        var shopspecid = $("#shop_spec_id").val();
        var number = $("#shop_number").html();
        var shopid = $("#shopid").val();
        var url = "/except/shopcontroller/addcart.action";
        $.ajax({
            url: url,
            type: 'post',
            dataType: "json",
            data: {
                json: jsonstr,
                shopid: shopid,
                num: number,
                sex: bady_sex_id,
                ageid: badyageid,
                shopspecpriceid: shopspecid
            },
            async: false,
            success: function (data) {
                var code = data.code;
                var message = data.message;
                var addressinfo = "";
                if (code == 10001 || code == 10002)
                    location.href = "/except/login.jsp";
                else if (code == 20000) {
                    $("#hint_message").html("加入成功");
                    $("#hintbg").show();
                } else {
                    $("#hint_message").html(message);
                    $("#hintbg").show();
                }
            }
        });
    } else
        return;
}

function add_buy() {
    if (spec) {
        var token = $("#token").val();
        if (isempty(token)) {
            location.href = "/except/login.jsp";
            return;
        }
        var jsonstr = "{token:" + token + "}";
        var bady_sex_id = 1;
        var badyageid = 1;
        var shopspecid = $("#shop_spec_id").val();
        var number = $("#shop_number").html();
        var shopid = $("#shopid").val();

        var cartinfo = "{num:" + number + ",shopcartid:null" +
            ",shopname:" + $("#shop_name").html() + ",shopid:" + shopid + "," +
            "shopspecid:" + shopspecid + ",age:" + badyageid + "," +
            "sex:" + bady_sex_id + ",img:\"" + $("#shop_img").attr("src") + "\"," +
            "specname:" + $("#specname").val() + ",price:" + $("#shop_price").html() + "}";
        if (isempty(cartinfo))
            return;
        else {
            cartinfo = "{shopinfo:[" + cartinfo + "]}";
            $("#shopinfo").val(cartinfo);
            $("#shopfrm").attr("action", "/except/files/indent/pre_order.jsp").submit();
            // location.href = "/except/files/indent/pre_order.jsp";
        }
    } else
        return;
}
